﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using flsfragusseModel;

public partial class partner_AddEditSalesPersonForDealer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        if (Session["fragusLeasingUserID"] != null && Session["fragusLeasingUserTypeID"] != null)
        {
            //   string test = Session["flspartnerwebtype"].ToString();
        }
        else
        {
            Response.Redirect("..\\default.aspx?error=1");
        }
        grdSalesMen.AutoGenerateColumns = false;
        if (!IsPostBack)
        {
          lblreferer.Text= Request.UrlReferrer.PathAndQuery.ToString();

            FillDealers();
            FillGrid();
        }
    }


    private void FillDealers()
    {
       flsfragusseEntities1 cEntity = new flsfragusseEntities1();
        drpDealers.DataSource = (from p in cEntity.tblDealer
                                 orderby p.dealer
                                 select
                                 new { Name = p.dealer, ID = p.dealerID });


        drpDealers.DataTextField = "Name";
        drpDealers.DataValueField = "ID";
        drpDealers.DataBind();

    }

    private void UpdateSalesMen(string username, string password,int dealeremployeeID)
    {
        flsfragusseEntities1 cEntity = new flsfragusseEntities1();

        List<tblDealerEmployee> lstdealerE = (from p in cEntity.tblDealerEmployee
                                              where p.dealerEmployeeID == dealeremployeeID
                                       select p).ToList();

        tblDealerEmployee de = null;
        if (lstdealerE.Count() > 0)
        {

            de = lstdealerE[0];

            de.dealerEmployee = username;
            de.pwd = password;

            cEntity.ObjectStateManager.ChangeObjectState(de, System.Data.EntityState.Modified);
            cEntity.SaveChanges();
        }
    }

    private void DeleteSalesMen(int dealeremployeeID)
    {
        flsfragusseEntities1 cEntity = new flsfragusseEntities1();

        List<tblDealerEmployee> lstdealerE = (from p in cEntity.tblDealerEmployee
                                              where p.dealerEmployeeID == dealeremployeeID
                                       select p).ToList();

        tblDealerEmployee de = null;
        if (lstdealerE.Count() > 0)
        {
            de = lstdealerE[0];
            cEntity.ObjectStateManager.ChangeObjectState(de, System.Data.EntityState.Deleted);
            cEntity.SaveChanges();
            
          
        }
    }
    private void InsertSalesMen(string username,string pwd)
    {
        flsfragusseEntities1 cEntity = new flsfragusseEntities1();

        tblDealerEmployee deEmpployee = new tblDealerEmployee();

        deEmpployee.dealerEmployee=username;
        deEmpployee.pwd = pwd;
        deEmpployee.date = DateTime.Now;
        deEmpployee.userID = 19;
        deEmpployee.dealerID = Convert.ToInt32(drpDealers.SelectedValue);
        cEntity.tblDealerEmployee.AddObject(deEmpployee);
       
        cEntity.SaveChanges();

    }
    private void FillGrid()
    {
         flsfragusseEntities1 cEntity = new flsfragusseEntities1();

        int dealerID=Convert.ToInt32(drpDealers.SelectedValue);
        grdSalesMen.DataSource = from p in cEntity.tblDealerEmployee
                             where p.dealerID== dealerID
                             select p;
        grdSalesMen.DataBind();

       
    }

    protected void drpDealers_SelectedIndexChanged(object sender, EventArgs e)
    {
        FillGrid();
    }
    protected void grdSalesMen_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdSalesMen.EditIndex = e.NewEditIndex;
        FillGrid();

    }
    protected void grdSalesMen_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        //Reset the edit index.
        grdSalesMen.EditIndex = -1;
        FillGrid();

    }
    //protected void grdSalesMen_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    //{
    //    //Retrieve the table from the session object.
    //    //DataTable dt = (DataTable)Session["TaskTable"];

    //    ////Update the values.
    //    //GridViewRow row = TaskGridView.Rows[e.RowIndex];
    //    //dt.Rows[row.DataItemIndex]["Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
    //    //dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
    //    //dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;

    //    ////Reset the edit index.
    //    //TaskGridView.EditIndex = -1;

    //    ////Bind data to the GridView control.
    //    //BindData();

    //}
    protected void grdSalesMen_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Insert"))
        {
            TextBox username = grdSalesMen.FooterRow.FindControl("txtUserName") as TextBox;
            TextBox pwd = grdSalesMen.FooterRow.FindControl("txtPassword") as TextBox;

            grdSalesMen.ShowFooter = false;
            InsertSalesMen(username.Text, pwd.Text);
            FillGrid();
        }

            if (e.CommandName.Equals("EmptyInsert"))
            {

                TextBox u = grdSalesMen.Controls[0].Controls[0].FindControl("txtUserName") as TextBox;
                TextBox p = grdSalesMen.Controls[0].Controls[0].FindControl("txtPassword") as TextBox;
                InsertSalesMen( u.Text, p.Text);
                FillGrid();
            }

        


        if(e.CommandName.Equals("Cancel"))
        {
            grdSalesMen.ShowFooter = false;
        }
       // if(e.CommandName.Equals("
           
     
            //     new AdminBAL().Insert(u.Text, p.Text, sp.Text, dT.SelectedValue, ad.Text, bool.Parse(dA.SelectedValue));
        //    lblError.Text = "<br />Record inserted successfully<br />";
         //   BindCurrentUsers(); // rebind the data} 

       // if (e.CommandName.Equals("EmptyInsert")){TextBox u = GridView1.Controls[0].Controls[0].FindControl("txtUserName") as TextBox;TextBox p = GridView1.Controls[0].Controls[0].FindControl("txtPassword") as TextBox;TextBox sp = GridView1.Controls[0].Controls[0].FindControl("txtSuperPassword") as TextBox;DropDownList dT = GridView1.Controls[0].Controls[0].FindControl("dropType") as DropDownList;TextBox ad = GridView1.Controls[0].Controls[0].FindControl("txtAdminDesc") as TextBox;DropDownList dA = GridView1.Controls[0].Controls[0].FindControl("dropActive") as DropDownList; new AdminBAL().Insert(u.Text, p.Text, sp.Text, dT.SelectedValue, ad.Text, bool.Parse(dA.SelectedValue));lblError.Text = "<br />Record inserted successfully<br />";BindCurrentUsers(); // rebind the dat}if (e.CommandName.Equals("Insert")){TextBox u = GridView1.FooterRow.FindControl("txtUserName") as TextBox;TextBox p = GridView1.FooterRow.FindControl("txtPassword") as TextBox;TextBox sp = GridView1.FooterRow.FindControl("txtSuperPassword") as TextBox;DropDownList dT = GridView1.FooterRow.FindControl("dropType") as DropDownList;TextBox ad = GridView1.FooterRow.FindControl("txtAdminDesc") as TextBox;DropDownList dA = GridView1.FooterRow.FindControl("dropActive") as DropDownList; new AdminBAL().Insert(u.Text, p.Text, sp.Text, dT.SelectedValue, ad.Text, bool.Parse(dA.SelectedValue));lblError.Text = "<br />Record inserted successfully<br />";BindCurrentUsers(); // rebind the data} 

    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        grdSalesMen.ShowFooter = true;
        FillGrid();

    }
    protected void grdSalesMen_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

      //  e.RowIndex
      //  string pw=p.Text;½
    
        GridViewRow row= grdSalesMen.Rows[e.RowIndex];

       int id=Convert.ToInt32(((TextBox)(row.Cells[0].Controls[0])).Text);
       string username= ((TextBox)(row.Cells[1].Controls[1])).Text;
       string pwd=((TextBox)(row.Cells[2].Controls[1])).Text;


       UpdateSalesMen(username, pwd,id);
       grdSalesMen.EditIndex = -1;
       FillGrid();
    }
   
    protected void grdSalesMen_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = grdSalesMen.Rows[e.RowIndex];

        int id= Convert.ToInt32(row.Cells[0].Text);
      

        DeleteSalesMen(id);
        grdSalesMen.EditIndex = -1;
        FillGrid();

    }
    protected void btn1_Click(object sender, EventArgs e)
    {
        Response.Redirect(lblreferer.Text);
    }
}